/**
 * 
 */
package vn.edu.hungvuongaptech.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import vn.edu.hungvuongaptech.common.Constant;
import vn.edu.hungvuongaptech.dao.TimeSheetDAO;

/**
 * @author ThanhHQ
 *
 */
public class DeleteTimeSheetController extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/* (non-Javadoc)
	 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	/* (non-Javadoc)
	 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		int roleId = Integer.parseInt(req.getSession().getAttribute(Constant.ROLE_ATT).toString());
		String nextPath = null;
		String[] ids = req.getParameterValues("chkTimeSheetID");
		if (ids != null) {
			int count = 0;
			for (int i = 0; i < ids.length; ++i) {
				count += TimeSheetDAO.removeTimeSheetById(Long.valueOf(ids[i]));
			}		
			if (count > 0) {
				switch (roleId) {
				case 1: //Staff role
					nextPath = Constant.PATH_RES.getString("timesheet.staff.messagePath") + "?msg=Selected timesheet's records are removed successfully!";
					break;
				case 2: //Manager role
					nextPath = Constant.PATH_RES.getString("timesheet.manager.messagePath") + "?msg=Selected timesheet's records are removed successfully!";
					break;
				case 3: //Admin role
					nextPath = Constant.PATH_RES.getString("timesheet.admin.messagePath") + "?msg=Selected timesheet's records are removed successfully!";
					break;
				}
			}else {
				switch (roleId) {
				case 1: //Staff role
					nextPath = Constant.PATH_RES.getString("timesheet.staff.messagePath") + "?msg=Unable to remove selected timesheet's records!";
					break;
				case 2: //Manager role
					nextPath = Constant.PATH_RES.getString("timesheet.manager.messagePath") + "?msg=Unable to remove selected timesheet's records!";
					break;
				case 3: //Admin role
					nextPath = Constant.PATH_RES.getString("timesheet.admin.messagePath") + "?msg=Unable to remove selected timesheet's records!";
					break;
				}
			}
		} else {
			switch (roleId) {
			case 1: //Staff role
				nextPath = Constant.PATH_RES.getString("timesheet.staff.messagePath") + "?msg=Please selects timesheet's record to remove!";
				break;
			case 2: //Manager role
				nextPath = Constant.PATH_RES.getString("timesheet.manager.messagePath") + "?msg=Please selects timesheet's record to remove!";
				break;
			case 3: //Admin role
				nextPath = Constant.PATH_RES.getString("timesheet.admin.messagePath") + "?msg=Please selects timesheet's record to remove!";
				break;
			}
		}
		resp.sendRedirect(nextPath);
	}
}
